
/********** Specification of Receiver Module ******************
 RF Receiver: RF_RX.c
 Signal Detection Method: sampling	( BIT recognition on the RISE ).
 Modulation of the signal: PWM.
 Header of the Signal: low level durring N mSec.
 Signal figure:   +----+         +-
                  |    |         |------->>>> BIT recognition on the RISE.
                 -+    +---------+
                  |    |    |    | 
                    Te   Te   Te
Te - Timing Element.                    
**************************************************************/
                  
#include <pic18.h>  
#include "my_macros.h"
#include "RF_RX.h"

#define 	CLOCK           			40			// MHz
#define 	TE            				150     	// us
#define 	OVERSAMPLING    			3			/* 2 or 3 */
#define 	PERIOD          			TE/OVERSAMPLING*CLOCK/4
#define	MIPS							CLOCK*1000L/4 				/* operations per 1 ms */
 
#define	LONG_HEAD_TIME_MSEC			4			// mSec

#define 	SHORT_HEAD   ( (LONG_HEAD_TIME_MSEC * MIPS) / (PERIOD * 1.3) )	  
//#define 	LONG_HEAD    ((LONG_HEAD_TIME_MSEC * 10000L) /(PERIOD/2))    
#define 	LONG_HEAD	SHORT_HEAD*2	

near unsigned char  RF_RecBuffer[RF_BUFFER_SIZE];              // receive buffer 

near unsigned char RFstate;           // receiver state
near signed char RFcount;           // timer counter
near unsigned char BytePointer;       // receive buffer pointer
near unsigned char BitCount;          // received bits counter

volatile near bit RF_Complete;       // buffer full
volatile near bit RFBit;             // sampled RF signal


#define	HIGH_TO     -(OVERSAMPLING*3)		// longest high Te
#define 	LOW_TO       (OVERSAMPLING*3)     // longest low  Te

enum
{	
	RF_RESET = 10,   	
	RF_SYNCHRO,   
	RF_ONE,      	
	RF_ZERO     	
};

void ReceiverEna ( void )
{
	RFstate = RF_RESET;         // reset state machine in all other cases
	RF_Complete = FALSE;        // start with buffer empty
	TMR3IF = 0;
	TMR3IE = 1;                   // TMR0 overflow interrupt
	// GIE = 1;                    // enable interrupts
}

void Init_RF_Receiver ( void )
{
	TMR3 = 0;
	TMR3ON = 1;	
	TRISF1 = INPUT;						  /* Input from RF receiver */
	TMR3IF = 0; 
	TMR3IE = 1;                  // TMR0 overflow interrupt
	RFstate = RF_RESET;          // reset state machine in all other cases
	RF_Complete = FALSE;        // start with buffer empty
}

void RF_Receiver ( void )
{
    // this routine gets called every time TMR0 overflows
    RFBit = RF_Inp;               // sampling RF pin verify!!!
    TMR3 = -PERIOD;             // reload
    TMR3IF = 0;

/*     if ( RF_Complete )                 // avoid overrun */
/*         return; */

    switch ( RFstate )            // state machine main switch
    {

    case RF_ONE:
        if ( RFBit == 0 )
        { // falling edge detected  ----+
          //                            |
          //                            +----
                if ( RFcount )		//  duration RF_ONE time must be minimum TWO samples
                	RFstate = RF_ZERO;
                else
                	 RFstate = RF_RESET;
        }
        else
        { // while high 
            RFcount--;
            if ( RFcount < HIGH_TO )
                RFstate = RF_RESET;      // reset if too long
        }
        break;

    case RF_ZERO:
        if ( RFBit )
        { // rising edge detected     +----
          //                          |
          //                      ----+
            RFstate = RF_ONE;
            RF_RecBuffer[BytePointer] >>= 1;              // rotate 
            if ( RFcount >= 0)
            {
                RF_RecBuffer[BytePointer] += 0x80;          // shift in bit
            }
            RFcount = 0;                // reset length counter
            
            if ( ( ++BitCount & 7) == 0 )
                BytePointer++;                 // advance one byte
            if (BitCount == BITS_NUMBER)
            {
                TMR3IE = 0; 					// Receiver DISABLE
					 RF_Complete = TRUE;
				 
					 //  ReceiverEna ();	
            }    
        }
        else
        { // still low
            RFcount++;
            if ( RFcount >= LOW_TO)     // too long low
            {
                RFstate = RF_SYNCHRO;      // fall back into RFSYNC state 
                BytePointer = 0;               // reset pointers, while keep counting on 
                BitCount = 0;
            }
        }
        break;

    case RF_SYNCHRO:
        if ( RFBit)
        { // rising edge detected  +---+                +---..
          //                       |   |  <-Theader->   |   
          //                           +----------------+
            if ( ( RFcount < SHORT_HEAD ) || ( RFcount >= LONG_HEAD ) )
            {
                RFstate = RF_RESET;
                break;                  // too short/long, no header
            }
            else
            {
                RFcount = 0;             // restart counter
                RFstate = RF_ONE;
            }
        }
        else
        { // still low
            RFcount++;
        }
        break;

    case RF_RESET:
    default:
        RFstate = RF_SYNCHRO;        // reset state machine in all other cases
        RFcount = 0;
        BytePointer = 0;
        BitCount = 0;
        break;
        
    } // switch
   
} // RF_Receiver 

        

    
    
